/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uta.tsrh.patient.misc;

import dbmanager.DBConfig;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author David
 */
public class SQLConnMgr {
    private  Connection connection = null;
    DBConfig cfg = new DBConfig();
                
                String ipAddress = cfg.getProperty("ipAddress");
                String dbPort = cfg.getProperty("mDbPort");
                String dbName = cfg.getProperty("mDbName");
                String dbUserName = cfg.getProperty("mDbUser");
                String dbPassword = cfg.getProperty("mDbPwds");
                
                String dbUrl = "jdbc:mysql://"+ipAddress+":"+dbPort+"/"+dbName;                
                
                
    /* This is a terrible, wretched thing to do, but I'm going to do it anyway for now */
//    private static final String url = "jdbc:mysql://localhost:3306/alertsystem";
//    private static final String user = "root";
//    private static final String password = "";
    
    public   Connection getConnection() {
        openDB();
        return connection;
    }
    
    private void openDB() {
        if (connection == null) {
            try {
                try {
                    Class.forName(cfg.getProperty("dbConnect"));
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(SQLConnMgr.class.getName()).log(Level.SEVERE, null, ex);
                }
                connection = DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
            } catch (SQLException ex) {
                System.out.println("Cannot connection to database!");
                Logger.getLogger(SQLMgr.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
